/*
 * Licensed under the MIT license:
 * http://www.opensource.org/licenses/mit-license.php
 */

package ru.ssau.berlin.entity;

import java.io.Serializable;
import java.util.List;
import javax.persistence.*;

/**
 *
 * @author Caesar
 */
@Entity
@NamedQueries({
    @NamedQuery(
        name = "findEventByName",
        query = "SELECT object(e) FROM Event as e WHERE e.name LIKE :eventName"
    ),
    @NamedQuery(
        name = "findAllEvents",
        query = "SELECT object(e) FROM Event as e"
    ),
    @NamedQuery(
        name = "findInstructorSchedule",
        query = "SELECT object(e) FROM Event as e WHERE :instructor MEMBER OF e.persons"
    ),
    @NamedQuery(
        name = "findGroupSchedule",
        query = "SELECT object(e) FROM Event as e " +
            "WHERE :group MEMBER OF e.groups " +
                "AND e.type.name IN ('Exam', 'Экзамен')"
    ),
    @NamedQuery(
        name = "findStudentSchedule",
        query = "SELECT object(e) FROM Event as e " +
            "WHERE ((:student MEMBER OF e.persons OR EXISTS (" +
                "SELECT g FROM e.groups as g WHERE :student MEMBER OF g.persons)) " +
                "AND EXISTS (SELECT ep FROM e.parameters as ep " +
                    "WHERE ep.attribute.type.name LIKE 'datetime' " +
                        "AND ep.attribute.name LIKE 'date' " +
                        "AND LOCATE(:day, ep.parameterValue) = 1))"
    )
})
public class Event implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;

    @ManyToOne
    private EventType type;

    @OneToMany(mappedBy = "event")
    private List<EventParameter> parameters;

    @ManyToMany(mappedBy = "events")
    private List<PersonGroup> groups;

    @ManyToMany(mappedBy = "events")
    private List<Person> persons;

    @OneToMany(mappedBy = "event")
    private List<PersonRole> roles;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<EventParameter> getParameters() {
        return parameters;
    }

    public EventType getType() {
        return type;
    }

    public void setType(EventType type) {
        this.type = type;
    }

    public List<PersonGroup> getGroups() {
        return groups;
    }

    public List<Person> getPersons() {
        return persons;
    }

    public List<PersonRole> getRoles() {
        return roles;
    }

    @PostLoad
    public void postLoad() {
        parameters.size();
        persons.size();
        groups.size();
        roles.size();
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Event)) {
            return false;
        }
        Event other = (Event) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return getName() + "[" + id + "]";
    }

}
